package com.feicheng.iou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.iou.commom.ReturnCode;
import com.feicheng.iou.entity.RolePermission;
import com.feicheng.iou.exception.SystemException;
import com.feicheng.iou.mapper.RoleMapper;
import com.feicheng.iou.mapper.RolePermissionMapper;
import com.feicheng.iou.pojo.UpdateRolePermissionPOJO;
import com.feicheng.iou.result.ResultInfo;
import com.feicheng.iou.service.RolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tianchunlei
 * @since 2021-06-28
 */
@Service
@Slf4j
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionMapper, RolePermission> implements RolePermissionService {

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * @Author wuweinan
     * @Description 查询用户对应的权限ID列表
     * @Date 2021-7-23 10:45
     * @param roleIds
     * @return List<Integer>
     */
    @Override
    public List<Integer> selectPermissionIdsByRoleIds(List<Integer> roleIds) {
        log.info("RolePermissionServiceImpl的selectPermissionIdsByRoleIds方法进入，参数roleIds：" + roleIds);

        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", roleIds).select("permission_id");
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);

        List<Integer> permissionIds = new ArrayList<>();
        for (RolePermission rolePermission : rolePermissions) {
            permissionIds.add(rolePermission.getPermissionId());
        }

        return permissionIds;
    }

    /**
     * @Author wuweinan
     * @Description 更新角色信息及其权限
     * @Date 2021-7-23 10:45
     * @param updateRolePermissionPOJO 封装角色信息和需要更新的权限Id集合
     * @return List<Integer>
     */
    @Override
    public ResultInfo updateRolePermission(UpdateRolePermissionPOJO updateRolePermissionPOJO) {

        log.info("RolePermissionServiceImpl的selectPermissionIdsByRoleIds方法进入，参数updateRolePermissionPOJO："
                + updateRolePermissionPOJO);

        //修改角色名
        int i = roleMapper.updateById(updateRolePermissionPOJO.getRole());

        if (i != 1){
            throw new SystemException(ReturnCode.CHANGE_ROLE_NAME_FAIL);
        }

        //删除角色对应的权限
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",updateRolePermissionPOJO.getRole().getRoleId());
        int delete = rolePermissionMapper.delete(queryWrapper);

        if(delete == 0){
            throw new SystemException(ReturnCode.DELETE_PERMISSION_FAIL);
        }

        //插入角色的权限
        Integer integer = rolePermissionMapper.
                insertRolePermission(updateRolePermissionPOJO.getRole().getRoleId(), updateRolePermissionPOJO.getPermissionIds());


        if(integer == 0){
            throw new SystemException(ReturnCode.INSERT_PERMISSION_FAIL);
        }

        return ResultInfo.success();
    }
}
